﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Web.group
{
    public partial class operatoradd : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Hashtable user = PhoneBook.Users.Users.get(Commons.StrUtil.FormatValue(Session["userid"]));

            DataSet roles = PhoneBook.Manage.SysRole.getGroupRole(user["groupid"].ToString());
            Commons.ControlUtil.BindDDL(ddl_roleid, roles, "rolename", "roleid");

            if (!IsPostBack)
            {
                if (user["IsBOOper"] == DBNull.Value || !Convert.ToBoolean(user["IsBOOper"]))
                {
                    DataSet ds = PhoneBook.Group.BranchOffice.list(Commons.StrUtil.FormatValue(user["groupid"]));

                    Commons.ControlUtil.BindDDL(ddlBO, ds, "boname", "id", "非分公司", "");
                }
                else
                {
                    Hashtable bo = PhoneBook.Group.BranchOffice.get(Commons.StrUtil.FormatValue(user["bo"]));

                    ddlBO.Items.Add(new ListItem(Commons.StrUtil.FormatValue(bo["boname"]), Commons.StrUtil.FormatValue(bo["id"])));

                    chkIsBOOper.Checked = true;
                    chkIsBOOper.Enabled = false;
                    ddlBO.Enabled = false;
                }
            }
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
            string strBoId = Request.Form["ctl00$ContentPlaceHolder1$ddlBO"] == null ? ddlBO.SelectedValue : Request.Form["ctl00$ContentPlaceHolder1$ddlBO"];

            Hashtable user = PhoneBook.Users.Users.get(Commons.StrUtil.FormatValue(Session["userid"]));

            if (PhoneBook.Users.Users.isExists(txb_username.Text.Trim()))
            {
                Helper.ShowMessage(u_message, controls.UCMessageType.attention, "用户名已存在");
                return;
            }

            if (!txb_userpwd.Text.Equals(txb_userpwd2.Text))
            {
                Helper.ShowMessage(u_message, controls.UCMessageType.attention, "两次的密码不一致");
                return;
            }

            

            Hashtable newuser = new Hashtable();
            newuser["username"] = txb_username.Text;
            Dx.ZcEncrypt.Encrypt3DES encrypt = new Dx.ZcEncrypt.Encrypt3DES();
            newuser["userpwd"] = txb_userpwd.Text;//encrypt.EncryptString(Dx.ZcEncrypt.Encrypt.GetMD5(txb_userpwd.Text));
            newuser["nickname"] = txb_nickname.Text;
            newuser["groupid"] = user["groupid"];

            //新增是否分公司管理员
            newuser["isbooper"] = chkIsBOOper.Checked;
            if (chkIsBOOper.Checked)
            {
                if (string.IsNullOrEmpty(strBoId))
                {
                    Helper.ShowMessage(u_message, controls.UCMessageType.attention, "请选择所属的分公司");
                    return;
                }
                newuser["bo"] = strBoId;
            }

            PhoneBook.Users.Users.addgroupuser(newuser);

            if (!string.IsNullOrWhiteSpace(ddl_roleid.SelectedValue))
            {
                string userid = PhoneBook.Users.Users.getByName(newuser["username"].ToString())["userid"].ToString();
                string roleid = ddl_roleid.SelectedValue;
                PhoneBook.Manage.SysUserRole.add(userid, roleid);
            }
            AddLog("新添加了管理员：{0};", newuser["username"]);
            Helper.ShowMessage(u_message, controls.UCMessageType.success, "保存成功");
        }
    }
}